package com.sxd.bill.common.util;

import java.util.ArrayList;
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.charset.StandardCharsets;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.sxd.bill.common.enums.LabelNameEnum;
import com.sxd.bill.common.item.LabelItem;
import com.sxd.bill.common.item.TextLabelItem;

public class JsonUtil {
    public ArrayList<TextLabelItem> readLocalFile(String path) {
        ArrayList<TextLabelItem> inputItems = new ArrayList<>();
        String jsonContent;
        try {
            jsonContent = new String(Files.readAllBytes(Paths.get(path)), StandardCharsets.UTF_8);
        } catch (IOException e) {
            e.printStackTrace();
            jsonContent = null;
        }
        JSONArray jsonArray = JSON.parseArray(jsonContent);
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            String text = jsonObject.getString("text");
            JSONArray labelsArray = jsonObject.getJSONArray("label");
            ArrayList<LabelItem> labelItemArray = new ArrayList<>();
            for (int j = 0; j < labelsArray.size(); j++) {
                JSONObject labelObject = labelsArray.getJSONObject(j);
                JSONArray labelNames = labelObject.getJSONArray("labels");
                String labelNameString = labelNames.getString(0);
                LabelNameEnum labelNameEnum;
                try {
                    labelNameEnum = LabelNameEnum.fromCnName(labelNameString);
                } catch (Exception e) {
                    // e.printStackTrace();
                    System.out.println(e.getMessage()); // 或者记录日志
                    continue;
                }

                int startIdx = labelObject.getInteger("start");
                int endIdx = labelObject.getInteger("end");
                // String entity = labelObject.getString("text");
                String entity = text.substring(startIdx, endIdx);
                LabelItem labelItem = new LabelItem();
                labelItem.setStartIndex(startIdx);
                labelItem.setEndIndex(endIdx);
                labelItem.setLabelName(labelNameEnum);
                labelItem.setLabelEntity(entity);

                labelItemArray.add(labelItem);
            }
            TextLabelItem inputItem = new TextLabelItem();
            inputItem.setText(text);
            inputItem.setLabelItems(labelItemArray);

            inputItems.add(inputItem);
        }
        return inputItems;
    }

    public static void saveObjectAsJson(Object obj, String path) {
        try {
            String jsonString = JSON.toJSONString(obj, SerializerFeature.PrettyFormat,
                    SerializerFeature.WriteMapNullValue);
            try (BufferedWriter writer = Files.newBufferedWriter(Paths.get(path), StandardCharsets.UTF_8)) {
                writer.write(jsonString);
            }
        } catch (IOException e) {
            // 处理异常
            e.printStackTrace();
        }
    }
}
